#include "VideoSettings.h"
#include <QStandardPaths>
#include <QDir>
#include <QDebug>

VideoSettings::VideoSettings(QObject *parent)
    : QObject(parent)
    , m_settings(nullptr)
{
    // 初始化QSettings
    QString configPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    QDir().mkpath(configPath);
    m_settings = new QSettings(configPath + "/video_settings.ini", QSettings::IniFormat, this);
    
    // 初始化默认值
    initializeDefaults();
    
    // 加载保存的设置
    loadSettings();
}

QObject *VideoSettings::GetInstance(QQmlEngine *, QJSEngine *)
{
    static VideoSettings settings;
    return &settings;
}

VideoSettings::~VideoSettings()
{
    // 析构时自动保存设置
    saveSettings();
}

void VideoSettings::setUri(const QString &uri)
{
    if (!uri.isEmpty() && m_uri != uri) {
        m_uri = uri;
        emit uriChanged(uri);
        emit settingsChanged();
    }
}

void VideoSettings::initializeDefaults()
{
    // 视频基本设置默认值
    m_uri = "TEST";
    m_width = 1920;
    m_height = 1080;
    m_frameRate = 30;
    m_bitRate = 5000000; // 5 Mbps
    m_timeout = 5000; // 5 seconds
    m_buffer = 1024; // 1KB
    m_cacheTime = 3000; // 3 seconds
}

// 视频基本设置 - Setters
void VideoSettings::setWidth(int width)
{
    if (m_width != width && width > 0) {
        m_width = width;
        emit widthChanged(width);
        emit resolutionChanged(resolution());
        emit settingsChanged();
    }
}

void VideoSettings::setHeight(int height)
{
    if (m_height != height && height > 0) {
        m_height = height;
        emit heightChanged(height);
        emit resolutionChanged(resolution());
        emit settingsChanged();
    }
}

void VideoSettings::setResolution(const QSize &resolution)
{
    if (resolution.isValid() && (m_width != resolution.width() || m_height != resolution.height())) {
        m_width = resolution.width();
        m_height = resolution.height();
        emit widthChanged(m_width);
        emit heightChanged(m_height);
        emit resolutionChanged(resolution);
        emit settingsChanged();
    }
}

void VideoSettings::setFrameRate(int frameRate)
{
    if (m_frameRate != frameRate && frameRate > 0) {
        m_frameRate = frameRate;
        emit frameRateChanged(frameRate);
        emit settingsChanged();
    }
}

void VideoSettings::setBitRate(int bitRate)
{
    if (m_bitRate != bitRate && bitRate > 0) {
        m_bitRate = bitRate;
        emit bitRateChanged(bitRate);
        emit settingsChanged();
    }
}

void VideoSettings::setTimeout(unsigned timeout)
{
    if (m_timeout != timeout) {
        m_timeout = timeout;
        emit timeoutChanged(timeout);
        emit settingsChanged();
    }
}

void VideoSettings::setBuffer(int buffer)
{
    if (m_buffer != buffer && buffer >= 0) {
        m_buffer = buffer;
        emit bufferChanged(buffer);
        emit settingsChanged();
    }
}

void VideoSettings::setCacheTime(int cacheTime)
{
    if (m_cacheTime != cacheTime && cacheTime >= 0) {
        m_cacheTime = cacheTime;
        emit cacheTimeChanged(cacheTime);
        emit settingsChanged();
    }
}

// 配置管理方法
void VideoSettings::loadSettings()
{
    if (!m_settings) return;
    
    m_settings->beginGroup("Video");
    m_width = m_settings->value("width", m_width).toInt();
    m_height = m_settings->value("height", m_height).toInt();
    m_frameRate = m_settings->value("frameRate", m_frameRate).toInt();
    m_bitRate = m_settings->value("bitRate", m_bitRate).toInt();
    m_timeout = m_settings->value("timeout", m_timeout).toUInt();
    m_buffer = m_settings->value("buffer", m_buffer).toInt();
    m_cacheTime = m_settings->value("cacheTime", m_cacheTime).toInt();
    m_settings->endGroup();
    
    emit settingsLoaded();
    qDebug() << "VideoSettings loaded from:" << m_settings->fileName();
}

void VideoSettings::saveSettings()
{
    if (!m_settings) return;
    
    m_settings->beginGroup("Video");
    m_settings->setValue("width", m_width);
    m_settings->setValue("height", m_height);
    m_settings->setValue("frameRate", m_frameRate);
    m_settings->setValue("bitRate", m_bitRate);
    m_settings->setValue("timeout", m_timeout);
    m_settings->setValue("buffer", m_buffer);
    m_settings->setValue("cacheTime", m_cacheTime);
    m_settings->endGroup();
    
    m_settings->sync();
    emit settingsSaved();
    qDebug() << "VideoSettings saved to:" << m_settings->fileName();
}

void VideoSettings::resetToDefaults()
{
    initializeDefaults();
    
    // 发送所有变更信号
    emit widthChanged(m_width);
    emit heightChanged(m_height);
    emit resolutionChanged(resolution());
    emit frameRateChanged(m_frameRate);
    emit bitRateChanged(m_bitRate);
    emit timeoutChanged(m_timeout);
    emit bufferChanged(m_buffer);
    emit cacheTimeChanged(m_cacheTime);
    
    emit settingsChanged();
    qDebug() << "VideoSettings reset to defaults";
}

QVariant VideoSettings::getSetting(const QString &key, const QVariant &defaultValue) const
{
    if (!m_settings) return defaultValue;
    return m_settings->value(key, defaultValue);
}

void VideoSettings::setSetting(const QString &key, const QVariant &value)
{
    if (!m_settings) return;
    m_settings->setValue(key, value);
    emit settingsChanged();
}

// 便捷方法
void VideoSettings::setResolution(int width, int height)
{
    setResolution(QSize(width, height));
}

QString VideoSettings::getResolutionString() const
{
    return QString("%1x%2").arg(m_width).arg(m_height);
}



QStringList VideoSettings::getCommonResolutions() const
{
    return QStringList() 
        << "640x480"   // VGA
        << "800x600"   // SVGA
        << "1024x768"  // XGA
        << "1280x720"  // HD
        << "1366x768"  // WXGA
        << "1920x1080" // Full HD
        << "2560x1440" // QHD
        << "3840x2160" // 4K UHD
        << "7680x4320"; // 8K UHD
}
